﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NTTDATA.BEENChina.CaseMonitor.Config;

namespace NTTDATA.BEENChina.CaseMonitor.UI
{
    public partial class FormConfig : Form
    {
        public FormConfig()
        {
            InitializeComponent();

            InitSerialPort();
            InitBaudRate();
            LoadConfig();

        }

        public void InitSerialPort()
        {
           String[] portNames = System.IO.Ports.SerialPort.GetPortNames();
           
           comboBoxSerialPort.Items.Clear();

           comboBoxSerialPort.Items.AddRange(portNames);
  
        }

         


        ///串口波特率列表。
        ///75,110,150,300,600,1200,2400,4800,9600,14400,19200,28800,38400,56000,57600, 
        ///115200,128000,230400,256000 
        public void InitBaudRate()
        {
            String[] baudRate = {"9600","14400","19200","28800","38400","56000","57600", "115200","128000"};

            comboBoxBaudRate.Items.Clear();
            comboBoxBaudRate.Items.AddRange(baudRate);
        }


        public void LoadConfig()
        {
            CommuniConfig config = null;
            try
            {
                 config = Config.CommuniConfig.LoadCommuniConfig();
            }
            catch (FileNotFoundException)
            {
                 
            }

            if (config != null)
            {
 
                this.comboBoxBaudRate.SelectedItem = config.BaudRate.ToString(CultureInfo.InvariantCulture);
                this.comboBoxSerialPort.SelectedItem = config.SerialPort.ToString(CultureInfo.InvariantCulture);

                this.textBoxCommuniPhones.Lines = config.CommuniPhones.ToArray() ;

                textBoxTimeSpan.Text = config.TimeSpan.ToString(CultureInfo.InvariantCulture);

                this.textBoxCase.Lines = config.CaseCheckerFiles.ToArray();
                
            }
        }

        public void SaveConfig()
        {
            CommuniConfig config = new CommuniConfig();
            config.BaudRate = int.Parse(this.comboBoxBaudRate.SelectedItem.ToString());
            config.SerialPort = this.comboBoxSerialPort.SelectedItem.ToString();

            config.CommuniPhones = this.textBoxCommuniPhones.Lines.ToList();
            config.TimeSpan = int.Parse(textBoxTimeSpan.Text);

            config.CaseCheckerFiles = this.textBoxCase.Lines.ToList();
           

            CommuniConfig.SaveToXml(config);


        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            SaveConfig();
            this.Close();
        }

        private void ToolStripMenuItemSelectFiles_Click(object sender, EventArgs e)
        {
            

            string textBoxName = this.contextMenuConfigFile.SourceControl.Name;

            OpenFileDialog openFileDialog  = new System.Windows.Forms.OpenFileDialog();

            openFileDialog.Multiselect = true;
            DialogResult result = openFileDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                string [] openFileNames = openFileDialog.FileNames;

                textBoxCase.Text = string.Empty;


                foreach (string openFileName in openFileNames)
                {
                    string file = System.IO.Path.GetFileName(openFileName);
                    if (textBoxCase.Lines.Any())
                        textBoxCase.AppendText( Environment.NewLine  );
                    textBoxCase.AppendText(file);
                }
                
                
                
            } 
        }
    }
}
